package org.itboys.core.service.mysql.front;

import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.core.common.SundayCommonConstants;
import org.itboys.core.dao.store.SundayStoreNumMapper;
import org.itboys.core.dto.order.SundayReProductOrderDto;
import org.itboys.core.entity.mysql.SundayProduct;
import org.itboys.core.entity.mysql.SundayReProductOrder;
import org.itboys.core.entity.mysql.SundaySku;
import org.itboys.core.entity.mysql.SundayStoreNum;
import org.itboys.core.service.mysql.web.SundayProductService;
import org.itboys.core.service.mysql.web.SundayReProductOrderService;
import org.itboys.core.service.mysql.web.SundaySkuService;
import org.itboys.mysql.service.BaseMySqlService;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @Author：贾琦磊
 * @Date：2018/8/24
 * @Description：移动端退货单
 */
@Service
public class SundayFrontReProductService extends BaseMySqlService<SundayReProductOrder>{
    @Autowired
    private SundayStoreNumMapper storeNumMapper;
    @Autowired
    private SundaySkuService skuService;
    @Autowired
    private SundayProductService productService;
    @Autowired
    private SundayReProductOrderService reProductOrderService;

    /**
     * 分页  根据仓库id获取该仓库所有库存商品
     * @param request
     * @return
     */
    public List<SundayReProductOrderDto> select(HttpServletRequest request){
        List<SundayReProductOrderDto> result=new ArrayList<>();
        Map<String, Object> param = ParamUtil.packageMysqlPageParam(request, "storeId");
        List<SundayStoreNum> storeNums = storeNumMapper.select(param);
        if(!ListUtil.isNotNull(storeNums)){return result;}
        Set<Long> ids=new HashSet<>();
        for (SundayStoreNum storeNum:storeNums
             ) {
            ids.add(storeNum.getSKUId());
        }
        param.clear();
        param.put("isDeleted", SundayCommonConstants.NO);
        param.put("ids",ids);
        List<SundaySku> skus = skuService.select(param);
        ids.clear();
        for (SundaySku sku:skus
             ) {
            ids.add(sku.getProductId());
        }
        param.clear();
        param.put("isDeleted", SundayCommonConstants.NO);
        param.put("ids",ids);
        List<SundayProduct> products = productService.select(param);
        for (SundayStoreNum storeNum:storeNums) {
            SundayReProductOrderDto dto=new SundayReProductOrderDto();
            dto.setNum(storeNum.getNum());
            for (SundaySku sku:skus) {
                if(storeNum.getSKUId().equals(sku.getId())){
                    dto.setRoughWeight(sku.getRoughWeight());
                    dto.setSKUId(sku.getId());
                    dto.setStandard(sku.getStandard());
                    for (SundayProduct product:products) {
                        if(sku.getProductId().equals(product.getId())){
                            dto.setName(product.getName());
                        }
                    }
                }
            }
            result.add(dto);
        }
        return result;
    }

    /**
     *  仓库退货，生成退货单，等待审核
     * @param preStoreId
     */
    public void clear(Long preStoreId){
        //生成退货单
        reProductOrderService.save(preStoreId);
    }
}
